package com.school.information.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.school.information.core.exception.ServiceException;
import com.school.information.entity.ModuleLabelEntity;
import com.school.information.enums.result.ModuleResultEnum;
import com.school.information.mapper.ModuleLabelMapper;
import com.school.information.qo.ModuleLabelQO;
import com.school.information.service.ModuleLabelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * (ModuleLabel)表服务实现类
 *
 * @author ycc
 * @since 2023-07-10 10:32:06
 */
@Service
@Slf4j
public class ModuleLabelServiceImpl extends ServiceImpl<ModuleLabelMapper, ModuleLabelEntity> implements ModuleLabelService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveInfo(ModuleLabelEntity moduleLabel) {
        ModuleLabelEntity labelName = getByNameAndModuleInfoId(moduleLabel.getName(), moduleLabel.getModuleInfoId());
        log.info("##labelName={}", labelName);
        if (Objects.nonNull(labelName) && StringUtils.isNotBlank(labelName.getName())) {
            log.error("##该模块下的标签名称已存在");
            throw new ServiceException(ModuleResultEnum.MODULE_LABEL_NAME_EXISTS);
        }
        return this.save(moduleLabel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateInfo(ModuleLabelEntity moduleLabel) {
        ModuleLabelEntity oldModuleLabel = getById(moduleLabel.getId());
        ModuleLabelEntity labelName = getByNameAndModuleInfoId(moduleLabel.getName(), moduleLabel.getModuleInfoId());
        log.info("##labelName={}", labelName);
        if (Objects.nonNull(labelName) && StringUtils.isNotBlank(labelName.getName())
                && !moduleLabel.getName().equals(oldModuleLabel.getName())) {
            log.error("##模块标签名称已存在");
            throw new ServiceException(ModuleResultEnum.MODULE_LABEL_NAME_EXISTS);
        }
        BeanUtil.copyProperties(moduleLabel, oldModuleLabel);
        return this.updateById(oldModuleLabel);
    }

    @Override
    public IPage<ModuleLabelEntity> pageList(ModuleLabelQO moduleLabel) {
        LambdaQueryWrapper<ModuleLabelEntity> queryWrapper = Wrappers.<ModuleLabelEntity>query().lambda().orderByDesc(ModuleLabelEntity::getUpdateTime);
        if (StringUtils.isNotBlank(moduleLabel.getName())) {
            queryWrapper.like(ModuleLabelEntity::getName, moduleLabel.getName());
        }
        if (null != moduleLabel.getModuleInfoId() && moduleLabel.getModuleInfoId() > 0) {
            queryWrapper.eq(ModuleLabelEntity::getModuleInfoId, moduleLabel.getModuleInfoId());
        }
        return this.page(new Page<>(moduleLabel.getCurrent(), moduleLabel.getSize()), queryWrapper);
    }

    @Override
    public ModuleLabelEntity getByName(String labelName) {
        return this.getOne(Wrappers.<ModuleLabelEntity>lambdaQuery().eq(ModuleLabelEntity::getName, labelName));
    }

    @Override
    public ModuleLabelEntity getByNameAndModuleInfoId(String labelName, Long moduleInfoId) {
        return this.getOne(Wrappers.<ModuleLabelEntity>lambdaQuery().eq(ModuleLabelEntity::getName, labelName)
                .eq(ModuleLabelEntity::getModuleInfoId, moduleInfoId));
    }
}
